const App = {
    data() {
        return {
            photo,
            stepData: [],
            stepLog: [],
            Operation_Step_SerialNumber: '',//当前进度
            nodeKey: '',
            showDialog: false,
            fileUrl:'',
        };
    },
    mounted() {
        //初始化
        let ls_URL = window.location.href;
        this.nodeKey = getQueryObject(ls_URL, "nodeKey");// 获取 nodeKey 参数
        this.getData();
    },
    computed: {
        num() {
            return this.stepData.findIndex(obj => obj.Operation_Step_SerialNumber === this.Operation_Step_SerialNumber) + 1;
        },
        log() {
            return (val) => {
                let obj = this.stepLog.filter(i => i.Operation_Step_SerialNumber == val)[0];
                return obj ? obj : false;
            }
        }
    },
    methods: {
        getData() {
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                method: "post",
                data: {
                    Data: 'party_member_operation_step',
                    Condition: "Row_Status='0' ORDER BY Operation_Step_Sequence + 0;",
                },
                success: (data) => {
                    this.stepData = data;
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                method: "post",
                data: {
                    Data: 'party_member_log',
                    Condition: "Application_SerialNumber='" + this.nodeKey + "' AND Row_Status='0' ORDER BY Operation_Datetime DESC LIMIT 1;",
                },
                success: (data) => {
                    if (data.length == 1) {
                        this.Operation_Step_SerialNumber = data[0].Operation_Step_SerialNumber;
                    }
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
            });

            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryBySQLSerialNumber`,
                type: "POST",
                data: {
                    SQLSerialNumber: "t_party_member_log",
                    Condition: window.btoa(unescape(encodeURIComponent(this.nodeKey))),
                },
                success: (data) => {
                    if (data.length >= 1) {
                        this.Operation_Step_SerialNumber = data[data.length - 1].Operation_Step_SerialNumber;
                        this.stepLog = data;
                    }
                    console.log(data);
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
        },
        handleEdit(row, index) {
            window.location.href = "./add.html?action=Add&index=" + index + "&nodeKey=" + this.nodeKey + "&Step_SerialNumber=" + row.Operation_Step_SerialNumber + "&ID_Card_Number=" + this.log(this.stepData[0].Operation_Step_SerialNumber).ID_Card_Number + "&Party_Organization_SerialNumber=" +  this.log(this.stepData[0].Operation_Step_SerialNumber).Party_Organization_SerialNumber;
        },
        handleEdit1(row, index) {
            // console.log(row);
            window.location.href = "./add.html?action=Modify&index=" + index + "&nodeKey=" + this.nodeKey + "&Step_SerialNumber=" + row.Operation_Step_SerialNumber + "&Party_Member_Log_SerialNumber=" + row.Party_Member_Log_SerialNumber;
        },
        async download(val) {
            if (val) {
                // const a = document.createElement('a');
                // a.href = "https://service.essp.top/mh/Image/" + val;
                // a.download = ("https://service.essp.top/mh/Image/" + val).split('/').pop(); // 从 URL 中提取文件名
                // document.body.appendChild(a);
                // a.click();
                // document.body.removeChild(a); // 移除创建的 a 标签
                // window.location.href = photo + val;
                let apiUrl = photo + val;
                let fileName = apiUrl.split('/').pop();
                
                try {
                    const response = await fetch(apiUrl);
                    const blob = await response.blob();
                    const url = window.URL.createObjectURL(blob);
                    
                    const a = document.createElement('a');
                    a.href = url;
                    a.download = fileName || 'download';
                    document.body.appendChild(a);
                    a.click();
                    
                    // 清理
                    setTimeout(() => {
                        document.body.removeChild(a);
                        window.URL.revokeObjectURL(url);
                    }, 100);
                } catch (error) {
                    console.error('下载失败:', error);
                }
            } else {
                this.$message.error({ message: '暂无可下载附件' })
            }
        },
        preview(val){
            this.fileUrl = photo + val;
            this.showDialog = true;
        },
        goBack() {
            window.history.back();
        },
    },
};
const { createApp, ref } = Vue;
const app = createApp(App);
app.use(ElementPlus, { locale: zhCn });
//这种方式是引入全部图标，无需单个引入
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component);
}
app.component('yl-upload', ylUpload);
app.component('yl-online-preview', ylOnlinePreview);

const ElMessage = ElementPlus.ElMessage;
app.mount("#app");
